
package com.android.finaltracer.channel;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

import com.android.finaltracer.util.FinalTracerIntents;

public class SmsReceiver extends BroadcastReceiver {
    private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    public static final String TAG_TYPE = Channel.FINALTRACER_TYPE;
    public static final String TAG_FROM = Channel.TAG_FROM;
    public static final String TAG_BODY = Channel.TAG_BODY;
    public boolean flag = false;

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (action == null || action.length() == 0) {
            return;
        }
        if (action.equals(ACTION_SMS_RECEIVED)) {
            SmsMessage[] msgs = getMessagesFromIntent(intent);
            SmsMessage sms = msgs[0];
            Log.i("Sms", "receive msg ");
            int pduCount = msgs.length;
            if (pduCount == 1) {
                String from = sms.getOriginatingAddress().startsWith("+86") ? sms
                        .getOriginatingAddress().substring(3) : sms
                        .getOriginatingAddress();
                String body = sms.getDisplayMessageBody().replace("(", "{")
                        .replace(")", "}");
                Log.i("Sms", "msg body=" + body);
                try {
                    JSONObject json = new JSONObject(body);
                    if (json.has(TAG_TYPE)) {
                        flag = true;
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (flag) {
                    abortBroadcast();
                    broadToSms(context, from, body);
                    Log.i("Sms", "broadcast to sms" + body);
                    flag = false;
                }
            }
        }

    }

    private SmsMessage[] getMessagesFromIntent(Intent intent) {

        Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
        byte[][] pduObjs = new byte[messages.length][];
        for (int i = 0; i < messages.length; i++) {
            pduObjs[i] = (byte[]) messages[i];
        }
        byte[][] pdus = new byte[pduObjs.length][];
        int pduCount = pdus.length;

        SmsMessage[] msgs = new SmsMessage[pduCount];
        for (int i = 0; i < pduCount; i++) {
            pdus[i] = pduObjs[i];
            msgs[i] = SmsMessage.createFromPdu(pdus[i]);
        }
        return msgs;
    }

    private void broadToSms(Context context, String from, String body) {
        Intent intent = new Intent(FinalTracerIntents.SMS_INTERACT).putExtra(
                TAG_FROM, from).putExtra(TAG_BODY, body);
        context.sendBroadcast(intent);
    }
}
